import urllib.request
import os
from lxml import etree

def get_content(page: int):
    page = f"_{page}" if page != 1 else ""
    url = f"https://sc.chinaz.com/tupian/qinglvtupian{page}.html"

    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36 Edg/133.0.0.0",
    }

    request = urllib.request.Request(url, headers=headers)

    response = urllib.request.urlopen(request)

    return response.read().decode("utf-8")

if __name__ == "__main__":
    start_page = int(input("请输入起始页码："))
    end_page = int(input("请输入结束页码："))

    for page in range(start_page, end_page + 1):
        content = get_content(page)

        tree = etree.HTML(content)

        img_url_list = tree.xpath("//body//img//@data-original")

        for index, img_url in enumerate(img_url_list):
            # //scpic1.chinaz.net/files/default/imgs/2024-06-24/b1e59313e780a040_s.jpg
            # https://scpic1.chinaz.net/files/default/imgs/2024-06-24/b1e59313e780a040_s.jpg
            filename = img_url.split("/")[-1]
            img_url = f"https:{img_url}"

            # 创建文件夹
            os.makedirs(f"站长图片_{page}", exist_ok=True)
            print(f"正在下载第{page}页的第{index + 1}张图片：{img_url}")
            # 下载文件到文件夹下
            urllib.request.urlretrieve(img_url, f"站长图片_{page}/{filename}")